package com.ruoyi.resume.mapper;

import com.ruoyi.resume.domain.model.Candidate;
import com.ruoyi.resume.domain.model.Job;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface JobMapper {

    // 获取分页或全部工作职位
    @Select("<script>" +
            "SELECT id, title, description, salary, city, experience, education, tags, filters,address, " +
            "created_at AS createdAt, updated_at AS updatedAt " +
            "FROM job " +
            "<if test='count != -1'>" +
            //"LIMIT #{start}, #{count} " +@Param("start") int start, @Param("count") int count
            "</if>" +
            "</script>")
    List<Job> findJobs();


    // 更新岗位候选人ID，将 JSON 字符串存储到 candidate_ids 字段
    /*@Update("UPDATE job SET candidate_ids = #{candidateIds} WHERE id = #{jobId}")
    void updateJobCandidateIds(@Param("jobId") int jobId, @Param("candidateIds") String candidateIds);*/

    // 根据岗位ID查询单个岗位信息
    @Select("SELECT id, title, description, salary, city, experience, education, tags, filters,address, " +
            "created_at AS createdAt, updated_at AS updatedAt " +
            "FROM job WHERE id = #{jobId}")
    Job findJobById(@Param("jobId") int jobId);

    // 更新岗位标签
    @Update("UPDATE job SET tags = #{tags} , filters = #{filters} WHERE id = #{jobId}")
    void updateJobLabels(@Param("jobId")Integer jobId, @Param("filters")String filters, @Param("tags") String tags);

    // 根据岗位ID查询候选人ID列表
    @Select("SELECT job_id as jobId,resume_id as resumeId,name,score FROM job_resume WHERE job_id = #{id}")
    List<Candidate> findCandidateIdsByJobId(@Param("id") int id);

    // 根据候选人id查询得分信息
    @Select("select * from job_resume where resume_id=#{id} and job_id=#{jobId}")
    Candidate findInfoByResumeId(@Param("jobId") int jodId,@Param("id") int id);

    // 插入候选人信息
    @Insert("INSERT INTO job_resume (job_id, resume_id, name, score) VALUES (#{jobId}, #{resumeId}, #{name}, #{score})")
    void insertJobCandidate(Candidate candidate);

    // 删除候选人信息(候选人为准)
    @Delete("DELETE FROM job_resume WHERE resume_id = #{resumeId}")
    void deleteJobCandidateByResumeId(@Param("resumeId") int resumeId);

    // 删除岗位候选人信息(岗位为准)
    @Delete("DELETE FROM job_resume WHERE job_id = #{jobId}")
    void deleteJobCandidateByJobId(int jobId);
}
